package com.iuie.ns.datum.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.datum.service.IExpenseService;
import com.iuie.ns.datum.vo.MthInstalmentDetailVO;
import com.iuie.ns.datum.vo.UserExpenseInfoVO;
import com.iuie.ns.datum.vo.UserExpenseInstalmentInfoVO;
import com.iuie.ns.datum.vo.UserExpenseStatisticInfoVO;
import com.iuie.ns.frame.utils.DateUtils;
import com.iuie.ns.frame.utils.DeviceUtil;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;

/**
 * 用户消费管理
 * @author liujie
 * @date 2018年4月6日下午4:30:12
 */
@RequestMapping("expense")
@Controller
public class UserExpenseController {
	
	@Resource
	private IExpenseService expenseService;

	/**
	 * 首页
	 * @return
	 * @date 2018年4月6日下午4:30:07
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request) {
		return toIndex(request);
	}
	
	/*
	 * 支持移动端
	 */
	private String toIndex(HttpServletRequest request) {
		if (DeviceUtil.isMobile(request)) {
			return "view-datum/expense/m_expense"; 
		}
		return "view-datum/expense/expense";
	}
	
	/**
	 * 新增页面
	 * @return
	 * @date 2018年4月6日下午4:29:57
	 */
	@RequestMapping("add/{relationId}/index")
	public String addIndex(@PathVariable("relationId") String relationId, HttpServletRequest request) {
		request.setAttribute("relationId", relationId);
		return "view-datum/expense/expenseAdd";
	}
	
	/**
	 * 修改页面
	 * @param id
	 * @return
	 * @date 2018年4月6日下午4:29:57
	 */
	@RequestMapping("update/{id}/index")
	public String updateIndex(@PathVariable("id") String id, HttpServletRequest request) {
		request.setAttribute("id", id);
		return "view-datum/expense/expenseAdd";
	}
	
	/**
	 * 详情页面
	 * @param id
	 * @param request
	 * @return
	 * @date 2018年4月21日下午5:31:17
	 */
	@RequestMapping("detail/{id}/index")
	public String detailIndex(@PathVariable("id") String id, HttpServletRequest request) {
		request.setAttribute("id", id);
		return "view-datum/expense/expenseDetail";
	}
	
	/**
	 * 实际还款时间修改页面
	 * @return
	 * @date 2018年4月30日下午3:56:56
	 */
	@RequestMapping("realRepayDate/update/{id}/index")
	public String realRepayDateUpdateIndex(@PathVariable("id") String id, HttpServletRequest request) {
		request.setAttribute("id", id);
		return "view-datum/expense/expenseRealRepayDateUpdate";
	}
	
	/**
	 * 查看月分期详情
	 * @param request
	 * @return
	 * @date 2018年4月30日下午5:05:10
	 */
	@RequestMapping("mthInstalmentDetail/{date}/index")
	public String viewMthInstalmentDetailIndex(HttpServletRequest request, @PathVariable("date") String date) {
		request.setAttribute("date", date);
		return "view-datum/expense/mthInstalmentDetail";
	}
	
	/**
	 * 根据条件分页查询
	 * @param condition
	 * @param page
	 * @return
	 * @date 2018年4月6日下午4:29:48
	 */
	@RequestMapping("find/page")
	@ResponseBody
	public ResponseInfo findByPage(UserExpenseInfoVO condition, Page page) {
		List<UserExpenseInfoVO> list = expenseService.findByPage(condition, page);
		return ResponseInfo.successInfo(list, page);
	}
	
	/**
	 * 保存一条记录
	 * @param item
	 * @return
	 * @throws IOException 
	 * @date 2018年4月6日下午5:49:24
	 */
	@RequestMapping("add/one")
	@ResponseBody
	public ResponseInfo addOne(UserExpenseInfoVO item, String targetId) throws IOException {
		expenseService.addOne(item, targetId);
		return ResponseInfo.successInfo("保存成功！");
	}
	
	/**
	 * 修改一条记录
	 * @param item
	 * @param id
	 * @return
	 * @date 2018年4月7日下午11:32:29
	 */
	@RequestMapping("update/{id}/one/{saveInstalmentDetail}")
	@ResponseBody
	public ResponseInfo updateOne(UserExpenseInfoVO item, @PathVariable("id") String id, @PathVariable("saveInstalmentDetail") String saveInstalmentDetail) {
		expenseService.updateOne(item, id, saveInstalmentDetail);
		return ResponseInfo.successInfo("修改成功！");
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 * @date 2018年4月6日下午5:57:51
	 */
	@RequestMapping("find/id/{id}")
	@ResponseBody
	public ResponseInfo findById(@PathVariable("id") String id) {
		UserExpenseInfoVO one = expenseService.findById(id);
		return ResponseInfo.successInfoByObject(one);
	}
	
	/**
	 * 根据ID删除
	 * @param id
	 * @return
	 * @date 2018年4月7日下午10:38:08
	 */
	@RequestMapping("delete/id/{id}")
	@ResponseBody
	public ResponseInfo deleteById(@PathVariable("id") String id) {
		expenseService.deleteById(id);
		return ResponseInfo.successInfo("删除成功！");
	}
	
	/**
	 * 查询统计信息
	 * @param condition
	 * @return
	 * @date 2018年4月14日下午4:37:23
	 */
	@RequestMapping("find/statistic/info")
	@ResponseBody
	public ResponseInfo findStatisticInfo(UserExpenseInfoVO condition) {
		UserExpenseStatisticInfoVO statisticInfo = expenseService.findStatisticInfo(condition);
		return ResponseInfo.successInfoByObject(statisticInfo);
	}
	
	/**
	 * 根据消费ID查询所有分期信息
	 * @param expenseId
	 * @param page
	 * @return
	 * @date 2018年4月22日下午3:45:27
	 */
	@RequestMapping("find/page/instalmentDetails/{expenseId}")
	@ResponseBody
	public ResponseInfo findAllInstalmentDetailsByPage(@PathVariable("expenseId") String expenseId, Page page) {
		List<UserExpenseInstalmentInfoVO> list = expenseService.findAllInstalmentDetails(expenseId, page);
		return ResponseInfo.successInfo(list, page);
	}
	
	/**
	 * 更新还款金额
	 * @param id
	 * @param repayAmount
	 * @return
	 * @date 2018年4月22日下午5:11:22
	 */
	@RequestMapping("update/repayAmount/{id}")
	@ResponseBody
	public ResponseInfo updateRepayAmount(@PathVariable("id") String id, Double repayAmount) {
		expenseService.updateRepayAmount(id, repayAmount);
		return ResponseInfo.successInfo("更新还款金额成功！");
	}
	
	/**
	 * 更新备注
	 * @param id
	 * @param remark
	 * @return
	 * @date 2018年4月22日下午5:44:31
	 */
	@RequestMapping("update/remark/{id}")
	@ResponseBody
	public ResponseInfo updateRepayAmount(@PathVariable("id") String id, String remark) {
		expenseService.updateRemark(id, remark);
		return ResponseInfo.successInfo("更新备注成功！");
	}
	
	/**
	 * 更新还款状态
	 * @param id
	 * @param isRepay
	 * @return
	 * @date 2018年4月22日下午5:56:37
	 */
	@RequestMapping("update/isRepay/{id}")
	@ResponseBody
	public ResponseInfo updateRepayState(@PathVariable("id") String id, String isRepay) {
		expenseService.updateRepayState(id, isRepay);
		return ResponseInfo.successInfo("更新还款状态成功！");
	}
	
	/**
	 * 更新实际还款日
	 * @param id
	 * @param isRepay
	 * @return
	 * @date 2018年4月30日下午4:03:51
	 */
	@RequestMapping("realRepayDate/update/{id}")
	@ResponseBody
	public ResponseInfo updateRealRepayDate(@PathVariable("id") String id, Date realRepayDate) {
		expenseService.updateRealRepayDate(id, realRepayDate);
		return ResponseInfo.successInfo("更新实际还款日为" + DateUtils.formatDate(realRepayDate, "yyyy-MM-dd") + "成功！");
	}
	
	/**
	 * 查询最近两个月的分期详情
	 * @param condition
	 * @param page
	 * @param date 以该月份为准，查询该月及下月的分期详情 
	 * @return
	 * @date 2018年4月30日下午5:32:04
	 */
	@RequestMapping("find/mthInstalmentDetail/page/{date}")
	@ResponseBody
	public ResponseInfo findMthInstalmentDetailByPage(MthInstalmentDetailVO condition, Page page, @PathVariable("date") String date) {
		List<MthInstalmentDetailVO> list = expenseService.findMthInstalmentDetailByPage(condition, page, date);
		return ResponseInfo.successInfo(list, page);
	}
	
}
